<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属公司" prop="companyId">
        <el-select
          v-model="queryParams.companyId"
          placeholder="所属公司"
          clearable
          style="width: 240px"
          @change="handleCompanyChange"
        >
          <el-option
            v-for="dict in companiesList"
            :key="dict.id"
            :label="dict.companyName"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="门窗类型" prop="typeId">
        <el-select
          v-model="queryParams.typeId"
          placeholder="门窗类型"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in typesList"
            :key="dict.id"
            :label="dict.typeName"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="型材" prop="materialId">
         <el-select
          v-model="queryParams.materialId"
          placeholder="型材"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in materialList"
            :key="dict.id"
            :label="dict.materialName"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['material-cutting-plan:formulas:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['material-cutting-plan:formulas:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['material-cutting-plan:formulas:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['material-cutting-plan:formulas:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="formulasList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <!-- <el-table-column label="所属公司ID" align="center" prop="companyId" />
      <el-table-column label="所属门窗类型ID" align="center" prop="typeId" /> -->
      <el-table-column label="所属公司" align="center" prop="companyName" />
      <el-table-column label="所属门窗类型" align="center" prop="typeName" />
      <el-table-column label="型材" align="center" prop="materialName" />
      <el-table-column label="公式内容(计算表达式)" align="center" prop="formulaContent" />
      <el-table-column label="材料数量" align="center" prop="materialCount" />
      <el-table-column label="备注信息" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['material-cutting-plan:formulas:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['material-cutting-plan:formulas:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改门窗用料计算公式对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属公司" prop="companyId">
          <el-select
            v-model="form.companyId"
            placeholder="所属公司"
            clearable
            style="width: 240px"
            @change="handleCompanyChange_update"
          >
            <el-option
              v-for="dict in companiesList"
              :key="dict.id"
              :label="dict.companyName"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="门窗类型" prop="companyId">
          <el-select
            v-model="form.typeId"
            placeholder="门窗类型"
            clearable
            style="width: 240px"
          >
            <el-option
              v-for="dict in typesListUpdate"
              :key="dict.id"
              :label="dict.typeName"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="型材" prop="materialId">
          <el-select
            v-model="form.materialId"
            placeholder="型材"
            clearable
            style="width: 240px"
          >
            <el-option
              v-for="dict in materialListUpdate"
              :key="dict.id"
              :label="dict.materialName"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="公式内容(计算表达式)"  prop="formulaContent">
          <el-input v-model="form.formulaContent" placeholder="公式内容(计算表达式)"  maxlength="100" />
        </el-form-item>
        <el-form-item label="材料数量" prop="materialCount">
          <el-input v-model="form.materialCount" placeholder="材料数量" maxlength="5"/>
        </el-form-item>
        <el-form-item label="备注信息" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" maxlength="100"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFormulas, getFormulas, delFormulas, addFormulas, updateFormulas } from "@/api/material-cutting-plan/formulas"
import { listTypes } from "@/api/material-cutting-plan/types"
import { listCompanies } from "@/api/material-cutting-plan/companies"
import { listMaterial } from "@/api/material-cutting-plan/material"

export default {
  name: "Formulas",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 门窗用料计算公式表格数据
      formulasList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        typeId: null,
        materialId: null,
        companyId: null,
        formulaName: null,
        formulaContent: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        companyId: [
          { required: true, message: "所属公司ID不能为空", trigger: "blur" }
        ],
        typeId: [
          { required: true, message: "所属门窗类型ID不能为空", trigger: "blur" }
        ],
        materialId: [
         { required: true, message: "型材id不能为空", trigger: "blur" }
        ],
        formulaContent: [
          { required: true, message: "公式内容(计算表达式)不能为空", trigger: "blur" },
          { max: 100, message: '长度不能多于100个字符', trigger: 'blur' }
        ],
        materialCount: [
          { required: true, message: "材料数量不能为空", trigger: "blur" },
          { pattern: /^\d+$/, message: '请输入正整数', trigger: 'blur' }
        ],
      },
      typesList: [],
      typesListUpdate:[],
      companiesList: [],
      materialList: [],
      materialListUpdate: [],
    }
  },
  created() {
    if(this.$route.query.companyId != null){
      this.queryParams.companyId = Number(this.$route.query.companyId)
    }
    if(this.$route.query.typeId != null){
      this.queryParams.typeId = Number(this.$route.query.typeId)
    }

    this.getList()
    this.getListCompanies()
  },
  watch: {
    // 根据名称筛选
    $route: function (to,from) {
      if(to.name!='Formulas'){
        return;
      }
      if(this.$route.query.companyId != null){
      this.queryParams.companyId = Number(this.$route.query.companyId)
    }
    if(this.$route.query.typeId != null){
      this.queryParams.typeId = Number(this.$route.query.typeId)
    }
      this.getList()
      this.getListCompanies()
      this.handleRouteChange()
    }
  },
  methods: {
    /** 查询门窗用料计算公式列表 */
    getList() {
      this.loading = true
      listFormulas(this.queryParams).then(response => {
        this.formulasList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 查询型材定义列表 */
    getListMaterial() {
      listMaterial().then(response => {
        this.materialList = response.rows
      })
    },
    handleRouteChange(){
      this.typesList =[];
      listTypes({companyId: this.queryParams.companyId,pageSize:9999}).then(response => {
        this.typesList = response.rows
      })
    },
    handleCompanyChange(value){
      console.log("companyId:"+value)
      this.typesList =[];
      this.materialList =[];
      this.queryParams.typeId = null;
      this.queryParams.materialId = null;
      if (value == null){
        return
      }
      listTypes({companyId: value,pageSize:9999}).then(response => {
        this.typesList = response.rows
      })
      listMaterial({companyId: value,pageSize:9999}).then(response => {
        this.materialList = response.rows
      })
    },
    handleCompanyChange_update(value){
      this.typesListUpdate =[];
      this.materialListUpdate = [];
      this.form.typeId = null;
      this.form.materialId = null;
      listTypes({companyId: value,pageSize:9999}).then(response => {
        this.typesListUpdate = response.rows
      })
      listMaterial({companyId: value,pageSize:9999}).then(response => {
        this.materialListUpdate = response.rows
      })
    },
        /** 查询公司基本信息列表 */
    getListCompanies() {
      listCompanies({pageSize:9999}).then(response => {
        this.companiesList = response.rows
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        materialId: null,
        typeId: null,
        formulaName: null,
        formulaContent: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.form.companyId = this.queryParams.companyId
      this.form.typeId = this.queryParams.typeId
      this.open = true
      this.title = "添加门窗用料计算公式"
      this.typesListUpdate = this.typesList

    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getFormulas(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改门窗用料计算公式"
        console.log("ddd")
        listTypes({companyId: this.form.companyId,pageSize:9999}).then(response => {
          this.typesListUpdate = response.rows
        })
         listMaterial({companyId: this.form.companyId,pageSize:9999}).then(response => {
          this.materialListUpdate = response.rows
        })
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFormulas(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addFormulas(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除门窗用料计算公式编号为"' + ids + '"的数据项？').then(function() {
        return delFormulas(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('material-cutting-plan/formulas/export', {
        ...this.queryParams
      }, `formulas_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
